/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.core.arrange.strategy;

import com.arisgo.cloud.attendance.clazz.core.arrange.MatchStore;
import com.arisgo.cloud.attendance.clazz.core.arrange.MatchStrategy;
import com.arisgo.cloud.attendance.clazz.entity.StaffClass;
import com.arisgo.cloud.attendance.clazz.mapper.StaffClassMapper;
import com.arisgo.cloud.attendance.clazz.vo.ClassInfoVo;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 全匹配 FullMatch
 *  > 时间以一天为准
 *  > 每个班都匹配所有员工
 *
 * @author xu keqi
 * @since 2025-03-29
 */
@Slf4j
@Component
@AllArgsConstructor
public class FullMatch implements MatchStrategy, InitializingBean {

    public static final String KEY = "fullMatch";

    private final MatchStore matchStore;

    private final StaffClassMapper staffClassMapper;

    @Override
    public void match(List<ClassInfoVo> infos, List<Long> staffIds) {
        if (CollectionUtils.isEmpty(infos) || CollectionUtils.isEmpty(staffIds)) {
            log.warn("排班策略 => 参数为空！");
            return;
        }
        log.info("排班策略 => 员工与班全匹配");
        Long tenantId = infos.get(0).getTenantId();
        List<StaffClass> staffClasses = Lists.newArrayList();
        for (ClassInfoVo classInfo : infos) {
            for (Long staffId : staffIds) {
                StaffClass staffClass = new StaffClass();
                staffClass.setTenantId(tenantId);
                staffClass.setStaffId(staffId);
                staffClass.setClassId(classInfo.getId());
                staffClasses.add(staffClass);
            }
        }
        if (!CollectionUtils.isEmpty(staffClasses)) {
            log.info("排班策略 => 保存关联数据");
            staffClassMapper.insertBatch(staffClasses);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        matchStore.add(KEY, this);
    }
}
